package com.blade.workstation.product.service.impl;

import com.blade.workstation.product.dto.ProductDTO;
import com.blade.workstation.product.entity.ProductEntity;
import com.blade.workstation.product.repository.ProductRepository;
import com.blade.workstation.product.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 产品信息服务实现类
 * 实现产品信息相关的业务逻辑
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductRepository productRepository;

    /**
     * 根据产品条码查询产品信息
     * @param barcode 产品条码（必填）
     * @return ProductDTO 包含产品详细信息，如果未找到返回null
     */
    @Override
    public ProductDTO getProductByBarcode(String barcode) {
        if (barcode == null || barcode.trim().isEmpty()) {
            return null;
        }

        ProductEntity entity = productRepository.findByBarcode(barcode).orElse(null);
        
        if (entity == null) {
            return null;
        }

        ProductDTO dto = new ProductDTO();
        BeanUtils.copyProperties(entity, dto);
        
        return dto;
    }
}

